<template>
  <div class="wrap">
    <div class="header">
      <div class="type-wrap" @click="PopTypeRefToggle">
        <span class="title">{{ currentSelect.name || '全部类型' }}</span>
        <van-icon name="apps-o" />
      </div>
      <div class="data-wrap">
        <span class="time" @click="PopMonthRefToggle">
          {{ currentTime }}
          <van-icon name="arrow-down" />
        </span>
        <span class="expense">总支出 ¥{{ totalExpense }}</span>
        <span class="income">总收入 ¥{{ totalIncome }}</span>
      </div>
    </div>
    <div class="content-wrap">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <CardItem v-for="(v, i) in list" :key="i" :bill="v"></CardItem>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="add" @click="addToggle">
      <van-icon name="records" />
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, toRefs, reactive } from 'vue';
import { get_list } from '@/api/index'
import dayjs from 'dayjs'
import { Toast } from 'vant'
interface dataType {
  currentSelect: currentSelectType,
  list: Array<itemType>
  [key: string]: any
}
interface currentSelectType {
  id?: number,
  name?: string,
  type?: string | number,
  user_id?: number
}
interface itemType {
  bills: Array<{
    amount: string,
    date: string,
    id: number,
    pay_type: number,
    remark: string,
    type_id: number,
    type_name: string
  }>,
  type_name: string

}
export default defineComponent({
  name: "Home",
  components: {

  },
  setup() {
    const PopTypeRef = ref()
    const PopMonthRef = ref()
    const PopAddRef = ref()
    const state = reactive<dataType>({
      currentSelect: {},
      currentTime: dayjs().format('YYYY-MM'),
      totalExpense: 0.0,
      totalIncome: 0.0,
      totalPage: 0,
      page: 0,
      page_size: 5,
      list: [],
      loading: false,
      finished: false,
      refreshing: false
    })
    onMounted(async () => { })
    const monthToggle = () => { }
    const getBillList = async () => {
      if (state.refreshing) {
        state.list = []
        state.refreshing = false
      }
      const params = {
        date: state.currentTime,
        page: state.page,
        page_size: state.page_size,
        type_id: state.currentSelect.id || 'all'
      }
      const { data } = await get_list(params)
      state.totalExpense = data.totalExpense
      state.totalIncome = data.totalIncome
      state.totalPage = data.totalPage
      state.list = state.list.concat(data.list)
      state.loading = false
      if (state.page >= state.totalPage) {
        state.finished = true
      }
    }
    const onLoad = () => {
      state.page = state.page + 1
      console.log(state.page)
      getBillList()
    }
    const onRefresh = () => {
      state.finished = false
      state.page = 0
      state.loading = true
      state.refreshing = true
      onLoad()
    }
    //类型弹窗切换
    const PopTypeRefToggle = () => {
      PopTypeRef.value.toggle()
    }
    //时间弹窗切换
    const PopMonthRefToggle = () => {
      PopMonthRef.value.toggle()
    }
    //时间弹窗切换
    const addToggle = () => {
      PopAddRef.value.toggle()
    }
    //选择类型
    const selectType = (item: currentSelectType) => {
      state.currentSelect = item
      onRefresh()
    }
    //选择时间
    const selectTime = (item: currentSelectType) => {
      state.currentTime = item
      onRefresh()
    }
    return {
      ...toRefs(state),
      onLoad,
      onRefresh,
      PopTypeRef,
      PopMonthRef,
      PopAddRef,
      PopTypeRefToggle,
      PopMonthRefToggle,
      addToggle,
      selectType,
      selectTime
    }
  }
})
</script>
<style lang="less" scoped>
.wrap {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 80px;
  .header {
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
    height: 80px;
    color: #fff;
    font-size: 14px;
    padding: 20px 20px;
    z-index: 100;
    box-sizing: border-box;
    background-color: #1989fa;
    .type-wrap {
      display: inline-block;
      padding: 6px;
      border-radius: 4px;
      position: relative;
      align-self: baseline;
      .title {
        margin-right: 22px;
      }
      .title::after {
        content: "";
        position: absolute;
        top: 12px;
        bottom: 11px;
        right: 32px;
        width: 1px;
        background-color: #e9e9e9;
      }
    }
    .data-wrap {
      margin-top: 10px;
      font-size: 13px;
      .time {
        margin-right: 12px;
        .sort-down {
          font-size: 12px;
        }
      }
      .expense {
        margin-right: 10px;
      }
    }
  }
  .content-wrap {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    overflow-y: auto;
  }
  .add {
    position: fixed;
    bottom: 100px;
    right: 30px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid #e9e9e9;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    background-color: #fff;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  }
}
</style>